/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include "LivemountJobManager.h"
#include "define/Types.h"
#include "log/Log.h"

using namespace std;

LivemountJobManager::LivemountJobManager()
{}

LivemountJobManager::~LivemountJobManager()
{}

LivemountJobManager& LivemountJobManager::GetInstance()
{
    static LivemountJobManager inst;
    return inst;
}

int LivemountJobManager::RegisterJob(const std::string& jobId, LivemountJobStatus status)
{
    INFOLOG("Enter RegisterJob %s, %d", jobId.c_str(), static_cast<int>(status));
    lock_guard<std::mutex> lk(m_mtx);
    if (m_jobStatusMap.count(jobId) > 0) {
        WARNLOG("Exist job id %s", jobId.c_str());
        return Module::FAILED;
    }
    m_jobStatusMap.emplace(jobId, status);
    return Module::SUCCESS;
}

int LivemountJobManager::UpdateJobStatus(const std::string& jobId, LivemountJobStatus status)
{
    lock_guard<std::mutex> lk(m_mtx);
    if (m_jobStatusMap.count(jobId) == 0) {
        ERRLOG("not found job id : %s", jobId.c_str());
        return Module::FAILED;
    }
    m_jobStatusMap[jobId] = status;
    return Module::SUCCESS;
}

LivemountJobStatus LivemountJobManager::GetJobStatus(const std::string& jobId)
{
    lock_guard<std::mutex> lk(m_mtx);
    if (m_jobStatusMap.count(jobId) == 0) {
        ERRLOG("not found job id : %s", jobId.c_str());
        return LivemountJobStatus::INVALID;
    }
    return m_jobStatusMap[jobId];
}

void LivemountJobManager::RemoveJob(const std::string& jobId)
{
    lock_guard<std::mutex> lk(m_mtx);
    if (m_jobStatusMap.count(jobId) == 0) {
        ERRLOG("not found job id : %s", jobId.c_str());
        return;
    }
    m_jobStatusMap.erase(jobId);
}